package com.study.juc;

import java.util.concurrent.TimeUnit;

class Test {
    private ThreadLocal<Integer> sales = ThreadLocal.withInitial(() -> 0);

    public void sale() {
        sales.set(sales.get() + 1);
    }

    public int getSales() {
        return sales.get();
    }
}

public class ThreadLocalDemo {
    public static void main(String[] args) {
        Test test = new Test();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                test.sale();
            }
            System.out.println("t1 = " + test.getSales());
        });
        t1.start();
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                test.sale();
            }
            System.out.println("t2 = " + test.getSales());
        });
        t2.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end = ");
    }
}
